package com.yihaodian.search.query;

import java.util.Collections;

import org.apache.lucene.search.Sort;
import org.apache.solr.request.LocalSolrQueryRequest;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.search.QueryParsing;

import com.yihaodian.index.solr.SolrSearcher;
import com.yihaodian.search.SearchContext;

public class SortBuilder {

	public void handle(SearchContext context) {
		// SortConverter.convert(searchRequest.getSort())

		String sortStr = context.getShardRequest().getSearchRequest().getSortString();
		Sort sort = null;
		if (sortStr != null) {
			SolrSearcher searcher = (SolrSearcher) context.getSearcher();
			SolrQueryRequest request = new LocalSolrQueryRequest(
					searcher.getSolrCore(), Collections.EMPTY_MAP);
			sort = QueryParsing.parseSort(sortStr, request);
		}

		if (sort == null)
			sort = new Sort(); // by score

		context.setSort(sort);
	}
}
